package com.qingzhuge.scheduler.manager;

import com.qingzhuge.common.annotation.ASysLog;
import com.qingzhuge.common.enums.EAuthorityType;
import com.qingzhuge.common.enums.EResourceType;
import com.qingzhuge.controller.BasePkLongController;
import com.qingzhuge.core.annotation.AResource;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.scheduler.dto.request.SysTaskScheduledRequestDto;
import com.qingzhuge.scheduler.dto.respone.SysTaskScheduledResponseDto;
import com.qingzhuge.scheduler.entity.SysTaskJob;
import com.qingzhuge.scheduler.entity.SysTaskScheduled;
import com.qingzhuge.scheduler.service.ISysTaskJobService;
import com.qingzhuge.scheduler.service.ISysTaskScheduledService;
import com.qingzhuge.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : zero.xiao
 * @description :
 * @date :2019-05-17 16:54
 * @modified :
 */
@ASysLog(value = "任务调度")
@AResource(name = "任务调度",type = EResourceType.menu, catalog = "任务管理")
@Api(tags = "S任务调度")
@RestController
@RequestMapping("api/sys/auth/task/scheduled")
public class SysTaskScheduledController extends BasePkLongController<SysTaskScheduled, ISysTaskScheduledService, SysTaskScheduledRequestDto, SysTaskScheduledResponseDto> {

    @Resource
    private ISysTaskJobService taskJobService;

    @Override
    public ResponseEntity<ResponseBodyDto<PageUtil<SysTaskScheduledResponseDto>>> queryPage(@RequestBody SysTaskScheduledRequestDto scheduled) {
        return super.queryPage(scheduled);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysTaskScheduledResponseDto>> get(@PathVariable Long id) {
        return super.get(id);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysTaskScheduledResponseDto>> save(@RequestBody SysTaskScheduledRequestDto scheduled) {
        return super.save(scheduled);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysTaskScheduledResponseDto>> update(@RequestBody SysTaskScheduledRequestDto scheduled) {
        return super.update(scheduled);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<Object>> delete(@PathVariable Long id) {
        return super.delete(id);
    }

    @AResource(name = "数据状态",type = EResourceType.button, authority = EAuthorityType.UPDATE,catalog = "操作")
    @ApiOperation(value = "数据状态")
    @PutMapping("/{id}/{status}")
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('UPDATE')")
    public ResponseEntity<ResponseBodyDto<Object>> status(@PathVariable Long id, @PathVariable Integer status){
        service.updateStatus(id, status);
        return returnSuccess();
    }

    @AResource(name = "作业列表",type = EResourceType.button, authority = EAuthorityType.READ,catalog = "操作")
    @GetMapping("/read/jobs")
    @ApiOperation(value = "作业列表")
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('READ')")
    public ResponseEntity<ResponseBodyDto<Object>> taskJobList() {
        List<SysTaskJob> records = taskJobService.findAll();
        return returnSuccess(records);
    }

    @AResource(name = "任务列表",type = EResourceType.button, authority = EAuthorityType.READ,catalog = "操作")
    @GetMapping("/read/tasks")
    @ApiOperation(value = "任务列表")
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('READ')")
    public ResponseEntity<ResponseBodyDto<Object>> list() {
        List<SysTaskScheduledResponseDto> records = service.getAllTaskDetail();
        return returnSuccess(records);
    }

    @AResource(name = "执行",type = EResourceType.button, authority = EAuthorityType.RUN,catalog = "操作")
    @PutMapping("/run/{id}")
    @ApiOperation(value = "立即执行任务")
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('RUN')")
    public ResponseEntity<ResponseBodyDto<Object>> exec(@PathVariable Long id) {
        service.execTask(id);
        return returnSuccess();
    }

    @AResource(name = "启动",type = EResourceType.button, authority = EAuthorityType.OPEN,catalog = "操作")
    @PutMapping("/open/{id}")
    @ApiOperation(value = "启动任务")
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('OPEN')")
    public ResponseEntity<ResponseBodyDto<Object>> open(@PathVariable Long id) {
        service.openTask(id);
        return returnSuccess();
    }

    @AResource(name = "暂停",type = EResourceType.button, authority = EAuthorityType.STOP,catalog = "操作")
    @PutMapping("/stop/{id}")
    @ApiOperation(value = "暂停任务")
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('STOP')")
    public ResponseEntity<ResponseBodyDto<Object>> stop(@PathVariable Long id) {
        service.stopTask(id);
        return returnSuccess();
    }
    @AResource(name = "删除",type = EResourceType.button, authority = EAuthorityType.DELETE,catalog = "操作")
    @DeleteMapping("/del/{taskGroup}/{taskName}")
    @ApiOperation(value = "删除任务")
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('DELETE')")
    public ResponseEntity<ResponseBodyDto<Object>> del(@PathVariable String taskGroup, @PathVariable String taskName) {
        service.delTask(taskName,taskGroup);
        return returnSuccess();
    }

    @AResource(name = "启动所有",type = EResourceType.button, authority = EAuthorityType.RUN,catalog = "操作")
    @PutMapping("/startAll")
    @ApiOperation(value = "启动所有")
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('RUN')")
    public ResponseEntity<ResponseBodyDto<Object>> startAll() {
        service.startAllTrigger();
        return returnSuccess();
    }

    @AResource(name = "暂停所有",type = EResourceType.button, authority = EAuthorityType.STOP,catalog = "操作")
    @PutMapping("/pauseAll")
    @ApiOperation(value = "暂停所有")
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('STOP')")
    public ResponseEntity<ResponseBodyDto<Object>> pauseAll() {
        service.pauseAllTrigger();
        return returnSuccess();
    }
}
